/***************************************************************************
 *   Copyright (C) 2008, 2009 by mj   *
 *   hanfman_cool@web.de   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

//steamserverutil.cpp - is a part from steamserverutil by MJ
//steamserverutil.googlecode.com

//steamserverutil.cpp is the main file of steamserverutil
//It handles the main functions


#include <iostream> //for cout and cin
#include <fstream> //for ifstream and ofstream
#include <stdlib.h> //for system()	
#include <string.h> //for strcpy, strcat and strcmp
#include <vector>

//for gettext
#include <libintl.h>
#include <locale.h>

#define _(STRING) gettext(STRING)



#include "mani.h"


//to make the difference of the arrays better 
#define OFF_N 0
#define OFF_C 1

using namespace std;


//version variable:
string _version = "v0.4";
string param;	//here are the params from function choice_params() saved and the path of the server for mani admin plugin
string workdir; //path to workdir
string home_dir; //path of home dir

string temp; //for user inputs etc..
char t[500]; //sometimes a char is needed

unsigned short int server[25]; //here are the "real" numbers of a server saved
int i = 1;	//is iterated by is_installed()

bool server_css, server_dods, server_garrysmod, server_hl2mp, server_tf, server_cs, server_czero, server_dmc, server_dod, server_gearbox, server_ricochet, server_tfc, server_darkmessiah, server_chivalry, server_diprip, server_insurgency, server_synergy, server_zps, server_esmod, server_l4d;
bool installed=false; //is set true when server is installed


//functions
void not_installed() {cout << _("\n\nServer not installed!");}
void never_happen()  {cerr << _("\n\nOops steamserverutil crashed!! Please make a bug report at steamseverutil.googlecode.com or launchpad.net/steamserverutil\nThank you!");}

//to handle the results from (int) system(str)
void system_check(int r)
{
	
	
	
	
	//0 means app exited with sucess
	//256 means screen -ls | grep &str exited without finding str, thats okay (look in screen.cpp)
	if(r == 0 || r == 256)
		return;
	else
	{
		cerr << "\nsystem_check(int r): r = " << r << endl;
		cerr << _("Failed to execute an external dependency!\n");
		switch(r)
		{
			case 2: cout << _("\nSeems like you killed a process with CTRL+C\n\n");
				return;
			case 512: cout << _("\nCannot find file!\n\n");
				return;
			case 16384:
				cout << _("\nSeems like you just pressed Enter when you were expected to enter a path\n\n");
				return;
			case 32512: cout << _("\nSeems like that screen/unzip/wget is not installed, please install it!\n\n");
				return;
			case 33280: cout << _("\nThis seems to be a bug of \"steam\" when it is closing, it is not critical!\n\n");
				return;
		}
		never_happen();
	}
}


//tests which server are installed
bool is_installed()
{
	ifstream in;
	
	//set workdir path
	temp = workdir + "/installed_servers";
	
	in.open(temp.c_str());
	
	if(!in.is_open())
	{
		cout << _("\n\nSorry, no servers found!\nInstall one!\n\n");
		return false;
	}
	
	
	cout << _("\n\nServers available: \n\n");
	
	installed=false; //set true when any server is installed

	//testing if games are installed

	i = 1;
	
	
	
	while (!in.eof())
	{
		in.getline(t, 500);
		
		if(!strncmp(t, "css", 3))
		
		{
			cout << i <<".\tCounter Strike Source\n";
			server[i] = 1;
			i++;
			server_css = true;
			installed = true;
		}
		
		if(!strncmp(t, "dods", 4))

		{
			cout << i <<".\tDay of Defeat Source\n";
			server[i] = 2;
			i++;
			server_dods=true;
			installed = true;
		}
	
		
		if(!strncmp(t, "garrysmod", 9))
		{
			cout << i <<".\tGarrys Mod\n";
			server[i] = 3;
			i++;
			server_garrysmod = true;
			installed = true;
		}
		
		
		if(!strncmp(t, "hl2mp", 5))
		{
			cout << i <<".\tHalf Life 2 Deathmatch\n";
			server[i] = 4;
			i++;
			server_hl2mp=true;
			installed = true;
		}
	
		
		if(!strncmp(t, "tf", 2))
		{
			cout << i <<".\tTeam Fortress\n";
			server[i] = 5;
			i++;
			server_tf = true;
			installed = true;
		}
		
		
		if(!strncmp(t, "cstrike", 7))
		{
			cout << i <<".\tCounter Strike\n";
			server[i] = 6;
			i++;
			server_cs=true;
			installed = true;
		}
		
		if(!strncmp(t, "cscz", 4))
		{
			cout << i <<".\tCounter Strike Condition Zero\n";
			server[i] = 7;
			i++;
			server_czero = true;
			installed = true;
		}
	
		
		if(!strncmp(t, "dmc", 3))
		{
			cout << i <<".\tDeathmatch Classic\n";
			server[i] = 8;
			i++;
			server_dmc=true;
			installed = true;
		}
				
			
		if(strncmp(t, "dods", 4))
		{
			if(!strncmp(t, "dod", 3))

			{
				cout << i <<".\tDay of Defeat\n";
				server[i] = 9;
				i++;
				server_dod = true;
				installed = true;
			}
		}
		
		
		if(!strncmp(t, "gearbox", 7))
		{
			cout << i <<".\tOpposing Force\n";
			server[i] = 10;
			i++;
			server_gearbox=true;
			installed = true;
		}
	
		
		if(!strncmp(t, "ricochet", 8))
		{
			cout << i <<".\tRicochet\n";
			server[i] = 11;
			i++;
			server_ricochet = true;
			installed = true;
		}
	
		
		if(!strncmp(t, "tfc", 3))
		{
			cout << i <<".\tTeam Fortress Classic\n";
			server[i] = 12;
			i++;
			server_tfc=true;
			installed = true;
		}
	
		
		if(!strncmp(t, "darkmessiah", 11))
		{
			cout << i <<".\tDark Messiah\n";
			server[i] = 13;
			i++;
			server_darkmessiah=true;
			installed = true;
		}
		
		if(!strncmp(t, "chivalry", 8))
		{
			cout << i <<".\tAge of Chivalry\n";
			server[i] = 14;
			i++;
			server_chivalry=true;
			installed = true;
		}
		
		if(!strncmp(t, "diprip", 6))
		{
			cout << i <<".\tD.I.P.R.I.P. Warm Up (HL2 Mod)\n";
			server[i] = 15;
			i++;
			server_diprip=true;
			installed = true;
		}
		
		if(!strncmp(t, "insurgency", 10))
		{
			cout << i <<".\tInsurgency (HL2 Mod)\n";
			server[i] = 16;
			i++;
			server_insurgency=true;
			installed = true;
		}
		
		if(!strncmp(t, "synergy", 7))
		{
			cout << i << "\tSynergy (HL2 Mod)\n";
			server[i] = 17;
			i++;
			server_synergy=true;
			installed = true;
		}
		
		if(!strncmp(t, "zps", 3))
		{
			cout << i <<".\tZombie Panic: Source (HL2 Mod)\n";
			server[i] = 18;
			i++;
			server_zps=true;
			installed = true;
		}
		
		if(!strncmp(t, "esmod", 5))
		{
			cout << i <<".\tEnternal Silence (Mod)\n";
			server[i] = 19;
			i++;
			server_esmod=true;
			installed = true;
		}
		
		if(!strncmp(t, "l4d", 3))
		{
			cout << i <<".\tLeft 4 Dead\n";
			server[i] = 20;
			i++;
			server_l4d=true;
			installed = true;
		}
		
		
	}
	in.close();
	
	
	if(installed)
		return true;
	else
		return false;
}

//lets the user choose the server
int choose_dialog()
{
	if(!is_installed())
		return -1;
	unsigned short int choice;
	cout << i << _(".\tBack\n");
	cout << " : ";
	cin >> choice;
	cin.ignore();
	
	if(choice == i)
		return -1;
	
	return choice;
}

void server_path(unsigned short int choice)
{
	switch(choice)
	{
		case 1:	if(server_css==true)
		{
						
			param += "servers_source/cstrike/";
			installed = true;
		}
		else
			not_installed();
		break;
		case 2:	if(server_dods==true)
		{
			param += "servers_source/dod/";
											
				
			installed = true;
		}
		else
			not_installed();
		break;
		case 3:	if(server_garrysmod==true)
		{
			param += "servers_source/garrysmod/";
									
					
			installed = true;
		}
		else
			not_installed();
		break;
		case 4:	if(server_hl2mp==true)
		{
			param += "servers_source/hl2mp/";
						
									
			installed = true;
		}
		else
			not_installed();
		break;
		case 5:	if(server_tf==true)
		{	
			param += "servers_source/tf/";
								
			installed = true;
		}
		else
			not_installed();
		break;
		case 6:	if(server_cs==true)
		{
			param += "servers_hl1/cstrike/";
									
									
			installed = true;
		}
		else
			not_installed();
		break;
		case 7:	if(server_czero==true)
		{
			param += "servers_hl1/czero/";
							
									
			installed = true;
		}
		else
			not_installed();
		break;
		case 8:	if(server_dmc==true)
		{
			param += "servers_hl1/dmc/";
				
						
			installed = true;
		}
		else
			not_installed();
		break;
		case 9:	if(server_dod==true)
		{
			param += "servers_hl1/dod/";
								
					
			installed = true;
		}
		else
			not_installed();
		break;
		case 10:	if(server_gearbox==true)
		{
			param += "servers_hl1/gearbox/";
									
			
			installed = true;
		}
		else
			not_installed();
		break;
		case 11:	if(server_ricochet==true)
		{
			param += "servers_hl1/ricochet/";
									
			
			installed = true;
		}
		else
			not_installed();
		break;
		case 12:	if(server_tfc==true)
		{
			param += "servers_hl1/tfc/";
									
			
			installed = true;
		}
		else
			not_installed();
		break;
		case 13:	if(server_darkmessiah==true)
		{

			param += "server_darkmessiah/";
									
			installed = true;
			
		}
		else
			not_installed();
		break;
		case 14:	if(server_chivalry==true)
		{

			param += "servers_source/ageofchivalry/";
									
			installed = true;
			
		}
		else
			not_installed();
		break;
		case 15:	if(server_diprip==true)
		{

			param += "servers_source/diprip/";
									
			installed = true;
			
		}
		else
			not_installed();
		break;
		case 16:	if(server_insurgency==true)
		{

			param += "servers_source/insurgency/";
									
			installed = true;
			
		}
		else
			not_installed();
		case 17:	if(server_synergy==true)
		{

			param += "servers_source/sunergy/";
									
			installed = true;
			
		}
		else
			not_installed();
		break;
		case 18:	if(server_zps==true)
		{

			param += "servers_source/zps/";
			installed = true;
			
		}
		else
			not_installed();
		break;
		case 19:	if(server_esmod==true)
		{

			param += "servers_source/esmod/";
			installed = true;
			
		}
		else
			not_installed();
		break;
		case 20:	if(server_l4d==true)
		{

			param += "servers_source/l4d/";
			installed = true;
			
		}
		else
			not_installed();
		break;
		default: cout << _("\n\nTry again ;)\n\n");
		break;
	}
}

void start_param(unsigned short int choice, char screen, bool *_installed)
{
	
	
	switch(choice)
	{
		case 1:	if(server_css==true)
		{
					
					
			*_installed = true;
					
			if(screen != 'y')
				param += "servers_source && ./srcds_run -game cstrike ";
			else
				param += "servers_source && screen -A -m -d -S css ./srcds_run -game cstrike ";
					

		}
		else
			not_installed();
		break;
		case 2:	if(server_dods==true)
		{
					
			*_installed = true;
			if(screen != 'y')
				param += "servers_source && ./srcds_run -game dod ";
			else
				param += "servers_source && screen -A -m -d -S dods ./srcds_run -game dod ";
		
			
			
				
					
		}
		else
			not_installed();
		break;

		case 3:	if(server_garrysmod==true)
		{
			*_installed = true;
			if(screen != 'y')
				param += "servers_source && ./srcds_run -game garrysmod ";
				
			else
				param += "servers_source && screen -A -m -d -S garrysmod ./srcds_run -game garrysmod ";
	
			
					
		}
		else
			not_installed();
		break;
		case 4:	if(server_hl2mp==true)
		{
			*_installed = true;
			if(screen != 'y')
				param += "servers_source && ./srcds_run -game hl2mp ";
			else
				param += "servers_source && screen -A -m -d -S hl2mp ./srcds_run -game hl2mp ";

	
				
					
		}
		else
			not_installed();
		break;
		case 5:	if(server_tf==true)
		{	
			*_installed = true;
			if(screen != 'y')
				param += "servers_source && ./srcds_run -game tf";
			else
				param += "servers_source && screen -A -m -d -S tf ./srcds_run -game zf ";

		}
		else
			not_installed();
		break;
		case 6:	if(server_cs==true)
		{
			*_installed = true;
			if(screen != 'y')
				param += "servers_hl1 && ./hlds_run -game cstrike ";
			
			else
				param += "servers_hl1 && screen -A -m -d -S cstrike ./srcds_run -game cstrike ";
				

			
					
		}
		else
			not_installed();
		break;
		case 7:	if(server_czero==true)
		{
			*_installed = true;
			if(screen != 'y')
				param += "servers_hl1 && ./hlds_run -game czero ";
			

			else
				param += "servers_hl1 && screen -A -m -d -S czero ./srcds_run -game czero ";

			
					
		}
		else
			not_installed();
		break;
		case 8:	if(server_dmc==true)
		{
			*_installed = true;
			if(screen != 'y')
				param += "servers_hl1 && ./hlds_run -game dmc ";
					

				
			else
				param += "servers_hl1 && screen -A -m -d -S dmc ./srcds_run -game dmc ";
					
		}
		else
			not_installed();
		break;
		case 9:	if(server_dod==true)
		{
			*_installed = true;
			if(screen != 'y')
				param += "servers_hl1 && ./hlds_run -game dod";
				

			else
				param += "servers_hl1 && screen -A -m -d -S dod ./srcds_run -game dod ";
		
					
		}
		else
			not_installed();
		break;
		case 10:	if(server_gearbox==true)
		{
			*_installed = true;
					
			if(screen != 'y')
				param += "servers_hl1 && ./hlds_run -game gearbox";
				

			
			else
				param += "servers_hl1 && screen -A -m -d -S gearbox ./srcds_run -game gearbox ";
				
			
				
		}
		else
			not_installed();
		break;
		case 11:	if(server_ricochet==true)
		{
			*_installed = true;
					
			if(screen != 'y')
				param += "servers_hl1 && ./hlds_run -game ricochet";
			

			
		
			else
				param += "servers_hl1 && screen -A -m -d -S ricochet ./srcds_run -game ricochet ";
			
		}
		else
			not_installed();
		break;
		case 12:	if(server_tfc==true)
		{
			*_installed = true;
					
			if(screen != 'y')
				param += "servers_hl1 && ./hlds_run -game tfc";
					
			else
				param += "servers_hl1 && screen -A -m -d -S tfc ./srcds_run -game tfc ";
			
					
			
		}
		else
			not_installed();
		break;
		case 13:	if(server_darkmessiah==true)
		{
			*_installed = true;

			if(screen != 'y')
				param += "server_darkmessiah && ./srcds_run ";
					

			else
				param += "server_darkmessiah && screen -A -m -d -S darkmessiah ./srcds_run ";
					
			
			
		}
		else
			not_installed();
		break;
				
		case 14:	if(server_chivalry==true)
		{
			*_installed = true;

			if(screen != 'y')
				param += "server_source && ./srcds_run -agme ageofchivalry ";
					

			else
				param += "server_darkmessiah && screen -A -m -d -S chivalry ./srcds_run -game ageofchivalry ";
					
			
			
		}
		else
			not_installed();
		break;
				
		case 15:	if(server_diprip==true)
		{
					
					
			*_installed = true;
					
			if(screen != 'y')
				param += "servers_source && ./srcds_run -game diprip ";
			else
				param += "servers_source && screen -A -m -d -S diprip ./srcds_run -game diprip ";
					

		}
		else
			not_installed();
		break;
		case 16:	if(server_css==true)
		{
					
					
			*_installed = true;
					
			if(screen != 'y')
				param += "servers_source && ./srcds_run -game insurgency ";
			else
				param += "servers_source && screen -A -m -d -S insurgency ./srcds_run -game insurgency ";
					

		}
		else
			not_installed();
		break;
		case 17:	if(server_synergy==true)
		{
					
					
			*_installed = true;
					
			if(screen != 'y')
				param += "servers_source && ./srcds_run -game synergy ";
			else
				param += "servers_source && screen -A -m -d -S synergy ./srcds_run -game synergy ";
					

		}
		else
			not_installed();
		break;
		case 18:	if(server_zps==true)
		{
					
					
			*_installed = true;
					
			if(screen != 'y')
				param += "servers_source && ./srcds_run -game zps ";
			else
				param += "servers_source && screen -A -m -d -S zps ./srcds_run -game zps ";
					

		}
		else
			not_installed();
		break;
		case 19:	if(server_esmod==true)
		{
					
					
			*_installed = true;
					
			if(screen != 'y')
				param += "servers_source && ./srcds_run -game esmod ";
			else
				param += "servers_source && screen -A -m -d -S esmod ./srcds_run -game esmod ";
					

		}
		else
			not_installed();
		break;
		case 20:	if(server_l4d==true)
		{
					
					
			*_installed = true;
					
			if(screen != 'y')
				param += "servers_source && ./srcds_run -game l4d ";
			else
				param += "servers_source && screen -A -m -d -S l4d ./srcds_run -game l4d ";
					

		}
		else
			not_installed();
		break;
		default: cout << _("\n\nWrong choice! Try again ;)\n\n");
		break;
	}
}






//installs servers
void install()
{


	bool exit=false;

	
	//set workdir path
	param = workdir + "/installed_servers";
	
	
	
	ifstream in(param.c_str()); //just to look if installed_servers exits or not
	if(!in.is_open())
	{
		cout << _("\n\nCreating directorys...\n");
		
		temp = "mkdir " + workdir + "/servers_source && mkdir " + workdir + "/servers_hl1";
		
		
		system_check(system(temp.c_str()));
	}
	in.close();
	
	
	ofstream out;
	out.open(param.c_str(), ios_base::app);
	
	while(!exit)
	{
		unsigned short int choice;
	
		
		cout << _("\n\nServers available for install: \n");
		cout << "1.  Counter Strike Source\n";
		cout << "2.  Day of Defeat Source\n";
		cout << "3.  Garrys Mod\n";
		cout << "4.  Half Life 2 Deathmatch\n";
		cout << "5.  Team Fortress\n";
		cout << "6.  Counter Strike\n";
		cout << "7.  Counter Strike Condition Zero\n";
		cout << "8.  Death Match Classic\n";
		cout << "9.  Day of Defeat\n";
		cout << "10. Opposing Force\n";
		cout << "11. Ricochet\n";
		cout << "12. Team Fortress Classic\n";
		cout << "13. Dark Messiah\n";
		cout << "14. Age of Chivalry\n";
		cout << "15. D.I.P.R.I.P. Warm Up (HL2 Mod)\n";
		cout << "16. Insurgency (HL2 Mod)\n";
		cout << "17. Synergy (HL2 Mod)\n";
		cout << "18. Zombie Panic: Source (HL2 Mod)\n";
		cout << "19. Enternal Silence (Mod)\n";
		cout << "20. Left 4 Dead\n";
		cout << _("21. EXIT to main menu\n");
		cout << " : ";
		cin >> choice;
		cin.ignore();
		
		cout << _("\n\nChecking currently installed servers...");
		is_installed();
		
		string _install;
		
		_install = workdir + "/";
		
		


		
		switch(choice)
		{
			case 1: 	cout << _("\n\nInstalling Counter Strike-Source Server...\nAdding to installed_server file\n\n");
			
			if(!server_css)
				out << "\ncss";
			_install += "./steam -command update -game \"Counter-Strike Source\" -dir " + workdir + "/servers_source/";
			
			system_check(system(_install.c_str()));
			break;
					
					
			case 2: 	cout << _("\n\nInstalling Day of Defeat Source Server...\nAdding to installed_server file\n\n");
			if(!server_dods)
				out << "\ndods";
			_install += "./steam -command update -game \"dods\" -dir " + workdir + "/servers_source/";
			
			
			system_check(system(_install.c_str()));
			break;
					
					
			case 3: 	cout << _("\n\nInstalling Garrys Mod Server...\nAdding to installed_server file\n\n");
			if(!server_garrysmod)
				out << "\ngarrysmod";
			_install += "./steam -command update -game \"garrysmod\" -dir " + workdir + "/servers_source/";
			
			system_check(system(_install.c_str()));
			break;
					
					
			case 4: 	cout << _("\n\nInstalling Half Life 2 Deathmatch Server...\nAdding to installed_server file\n\n");
			if(!server_hl2mp)
				out << "\nhl2mp";
			_install += "./steam -command update -game \"hl2mp\" -dir " + workdir + "/servers_source/";
			
			system_check(system(_install.c_str()));
			break;
					
					
					
			case 5: 	cout << _("\n\nInstalling Team Fortress Server...\nAdding to installed_server file\n\n");
			if(!server_tf)
				out << "\ntf";
			_install += "./steam -command update -game \"tf\" -dir " + workdir + "/servers_source/";
			
			
			system_check(system(_install.c_str()));
			break;
					
					
			case 6: 	cout << _("\n\nInstalling Counter Strike Server...\nAdding to installed_server file\n\n");
			if(!server_cs)
				out << "\ncstrike";
			_install += "./steam -command update -game \"cstrike\" -dir " + workdir + "/servers_hl1/";
			system_check(system(_install.c_str()));
			break;
					
					
			case 7: 	cout << _("\n\nInstalling Counter Strike Condition Zero Server...\nAdding to installed_server file\n\n");
			if(!server_czero)
				out << "\ncscz";
			_install += "./steam -command update -game \"czero\" -dir " + workdir + "/servers_hl1/";
			
			
			system_check(system(_install.c_str()));
			break;
					
					
			case 8: 	cout << _("\n\nInstalling Death Match Classic Server...\nAdding to installed_server file\n\n");
			if(!server_dmc)
				out << "\ndmc";
			_install += "./steam -command update -game \"dmc\" -dir " + workdir + "/servers_hl1/";
			
			system_check(system(_install.c_str()));
			break;
					
					
			case 9: 	cout << _("\n\nInstalling Day of Defeat Server...\nAdding to installed_server file\n\n");
			if(!server_dod)
				out << "\ndod";
			_install += "./steam -command update -game \"dod\" -dir " + workdir + "/servers_hl1/";
			
			
			system_check(system(_install.c_str()));
			break;
					
					
			case 10: 	cout << _("\n\nInstalling Opposing Force Server...\nAdding to installed_server file\n\n");
			if(!server_gearbox)
				out << "\ngearbox";
			_install += "./steam -command update -game \"gearbox\" -dir " + workdir + "/servers_hl1/";
			
			system_check(system(_install.c_str()));
			break;
					
					
			case 11: 	cout << _("\n\nInstalling Ricochet Server...\nAdding to installed_server file\n\n");
					
			if(!server_ricochet)
				out << "\nricochet";
			_install += "./steam -command update -game \"ricochet\" -dir " + workdir + "/servers_hl1/";
			
			system_check(system(_install.c_str()));
			break;
					
					
			case 12: 	cout << _("\n\nInstalling Team fortress Classic Server...\nAdding to installed_server file\n\n");
			if(!server_tfc)
				out << "\ntfc";
			_install += "./steam -command update -game \"tfc\" -dir " + workdir + "/servers_hl1/";
			
			
			system_check(system(_install.c_str()));
			break;
					
					
			case 13: 	cout << _("\n\nInstalling Dark Messiah Server...\nCreating directory...\nAdding to installed_server file\n\n");
						
			
			if(!out.is_open())
			{
				
				temp = "mkdir " + workdir + "/server_darkmessiah/";
				system_check(system(temp.c_str()));
			}
					
			if(!server_darkmessiah)
				out << "\ndarkmessiah";
			_install += "./steam -command update -game \"darkmessiah\" -dir " + workdir + "/server_darkmessiah/";
			
			system_check(system(_install.c_str()));
			break;
					
					
			case 14: 	cout << _("\n\nInstalling Age of Chivalry Server...\n\nAdding to installed_server file\n\n");
			if(!server_chivalry)
				out << "\nchivalry";
			_install += "./steam -command update -game \"ageofchivalry\" -dir " + workdir + "/servers_source/";
			
			system_check(system(_install.c_str()));
			break;
					
					
			case 15: 	cout << _("\n\nInstalling D.I.P.R.I.P. Warm Up (HL2 Mod) Server...\nAdding to installed_server file\n\n");
			if(!server_diprip)
				out << "\ndiprip";
			_install += "./steam -command update -game \"diprip\" -dir " + workdir + "/servers_source/";
			
			system_check(system(_install.c_str()));
			break;
					
					
			case 16: 	cout << _("\n\nInstalling Insurgency (HL2 Mod) Server...\nAdding to installed_server file\n\n");
			if(!server_insurgency)
				out << "\ninsurgency";
			_install += "./steam -command update -game \"insurgency\" -dir " + workdir + "/servers_source/";
			system_check(system(_install.c_str()));
			break;
					
					
			case 17: 	cout << _("\n\nInstalling Synergy (HL2 Mod) Server...\nAdding to installed_server file\n\n");
			if(!server_synergy)
				out << "\nsynergy";
			_install += "./steam -command update -game \"synergy\" -dir " + workdir +  "/servers_source/";
			
			system_check(system(_install.c_str()));
			break;
					
					
			case 18: 	cout << _("\n\nInstalling Zombie Panic: Source (HL2 Mod) Server...\nAdding to installed_server file\n\n");
			if(!server_zps)
				out << "\nzps";
			_install += "./steam -command update -game \"zps\" -dir " + workdir + "/servers_source/";
			
			system_check(system(_install.c_str()));
			break;
			
			case 19:cout << _("\n\nInstalling Counter Eternal Silence (mod)...\nAdding to installed_server file\n\n");
			
			if(!server_esmod)
				out << "\nesmod";
			_install += "./steam -command update -game \"esmod\" -dir " + workdir + "/servers_source/";
			system_check(system(_install.c_str()));
			
			break;
			
			case 20:
				cout << _("\n\nInstalling Left 4 Dead...\nAdding to installed_server file\n\n");
			
				if(!server_l4d)
					out << "\nl4d";
				_install += "./steam -command update -game \"l4d_full\" -dir " + workdir + "/servers_source/";
				system_check(system(_install.c_str()));
				break;
					
			case 21: 	exit=true;
			break;
					
					
			default: 	cout << _("\n\nTry again;)\n\n");
			break;
		}
		
		out.close();
		
	}
	
}


//adjustements for server
void param_choice()
{


	char choice;



	cout << _("\n\nDisable Valve Anti Cheat System?(y|n) ");
	cin >> choice;

	cin.ignore();

	
	if(choice == 'y')
	{
		param += "-insecure ";
	}
	
	cout <<_("\n\nDisable bots?(y|n) ");
	cin >> choice;

	cin.ignore();

	if(choice == 'y')
	{
		param += "-nobots ";
	}
	
	cout << _("\n\nSpecify IP?(y|n) ");
	cin >> choice;

	cin.ignore();

	if(choice == 'y')
	{
		cout <<_("\n\nIP: ");
		getline(cin, temp);
//		cin.ignore();

		param += "-ip " + temp + " ";
	}

	
	
	cout << _("\n\nSpecify port?(y|n) ");
	cin >> choice;
	cin.ignore();

	if( choice=='y')
	{
		cout <<_("\n\nPort: ");
		getline(cin, temp);

		param += "-port " + temp + " ";	
	}

//	cin.ignore();

	
	cout << _("\n\nMaximal Players? ");
	getline(cin, temp);

	
	param += "+maxplayers " + temp + " ";

//	cin.ignore();


	cout << _("\n\nWhich map? ");
	getline(cin, temp);


	param += "+map " + temp + " ";

//	cin.ignore();


	cout << _("\n\nOnly Lan Server?(y|n) ");
	cin >> choice;

	cin.ignore();
	
	
	if(choice=='y')
	{
		param += "+sv_lan 1 ";
	}
	
	cout << _("\n\nSpecify hostname?(y|n) ");
	cin >> choice;

	cin.ignore();
	

	if (choice == 'y')
	{
		cout << _("\n\nHostname: ");
		getline(cin, temp);
		param += "+hostname " + temp;
	}

//	cin.ignore();
	
	
	

}


//here you can start a server
void start()
{

			


	bool _installed = false;


	cout << _("\n\nWhich server should be started?\n\n");
	
	int choice = choose_dialog();
	
	if(choice == -1)
		return;
	
	
			//set workdir path
	param = "cd " + workdir + "/";
	
	
	
	cout << _("\n\nWhen you have \"screen\" installed, you can start a server in background, independent of the steamserverutil session.\n");
	cout << _("Start the server in background with screen?(y|n) ");
	char screen;
	cin >> screen;

	start_param(server[choice], screen, &_installed); //saves in char param[]
	
	
	if( _installed == true)
	{	
		
		param_choice(); //saves in char param[]
		param += " +exec server.cfg";
		
		
	
		//here the server starts
	
		cout << _("\nStarting the server...");
		
		cout << flush; //the message should come before the server starts ;)
		
		system_check(system(param.c_str()));
	}
}


//here you can adjust and start your favorites


void favorites(int id = -1)
{

	


	int j;
	
	

	
	bool exit = false;
	
	//read .steamserverutil/favorites and save entrys in variables
	while(!exit)
	{

		vector<string> input[2];
		
		j=-1;	

		ifstream in;
		
		//set workdir path
		param = workdir + "/favorites";
				
		in.open(param.c_str());
		cout << _("\n\nReading favorites...\n\n");
	
		if(in.is_open())
		{

						
	

	
			while(!in.eof())
			{
	
				
				
				getline(in, temp);
	
				switch(temp[0])
				{
					case 'n': if(temp[1] == ':')
					{
						j++;
						input[OFF_N].push_back(&(temp[3]));
							
					}
					else
						cout << _("Your .favorites is incorrupt, cannot understand: ") << temp << _("\nIgnoring...\n");
					break;
					case 'c': if(temp[1] == ':')
					{
						input[OFF_C].push_back( &(temp[3]));
					}
					else
						cout << _("Your .favorites is incorrupt, cannot understand: ") << temp << _("\nIgnoring...\n");
					break;
					default: if(temp[0] == 10 || temp[0]== 0) //10 means new line 0 means end of string
					{
							//nothing to do
					}
					else
						cout << _("Your .favorites is incorrupt, cannot understand: ") << temp << _("\nIgnoring...\n");
					break;
				}
				
				
			}
	
			
			
		}
		else
		{
			cout << _("You have no favorites, please add one!\n\n");
		}
	
		in.close();
		


		if(id != -1)
		{
			if(id > j)
			{
				cout << _("\n\nThis entry does not exist!\n");
				return;
			}	
			//else
			cout << _("Starting: ") << input[OFF_N][id] << endl;
			system_check(system(input[OFF_C][id].c_str()));
			return;
		}




		//reading is finished, now user can adjust favorites
		int choice;
		string params;
		string name;
		
		bool installed = false; //is set true when server is installed

		ofstream save;
		save.open(param.c_str(), ios_base::app);
		
		cout << _("\n\n1. Add favorite\n");
		cout << _("2. Delete favorite\n");
		cout << _("3. Delete all favorites\n");
		cout << _("4. Start favorite\n");
		cout << _("5. Make a .desktop entry\n");
		cout << _("6. Back to main menu\n");
		cout << " : ";
		cin >> choice;

		

		switch(choice)
		{
			

			case 1: cout << _("\n\nFrom which game should be the favorite?\n\n");


			choice = choose_dialog();
			
			if(choice == -1)
				break;
	
			params =  "cd " + workdir + "/";
			
	
			cout << _("\n\nWhen you have \"screen\" installed, you can start a server in background, independent of the steamserverutil session.\n");
			cout << _("Start the server in background with screen?(y|n) ");
			char screen;
			cin >> screen;
			
			
			

			param = ""; //clear it
			start_param(server[choice], screen, &installed);
					

					

			if(installed==true)
			{
				cout << _("\n\nNow you should make a few adjustments for the favorite\n\n");
				
				params += param; //first the value of start_param(unsigned short int, char, *bool)
				
				param = ""; //clear it
				param_choice();

				
				params += param;
					
				cout << _("\n\nWhich name do you wish for this favorite? ");
				getline(cin, name);
				

				//cin.ignore();

				save << "n: " << name << endl;
				save << "c: " << params << endl;
				save.close();
			}

			break;


			case 2: cout << _("\n\nWhich favorite server do you want to delete?\n\n");
			for (int i = 0; i<=j;i++)
			{
				cout << (i+1) << "\t" <<  input[OFF_N][i] << endl;
			}
				
			cout << " : ";
			cin >> choice;
				
			choice--;


			if(choice > j)
			{
				cout << _("\n\nThis entry does not exist!");
			}
			else
			{

				ofstream save_del;
				save_del.open(param.c_str());


				input[OFF_N].erase(input[OFF_N].begin() + choice);
				input[OFF_C].erase(input[OFF_C].begin() + choice);


				for (int i = 0;i<=input[OFF_N].size();i++)
				{
					save_del << "n: " << input[OFF_N][i] << endl;
					save_del << "c: " << input[OFF_C][i] << endl;
					
				}
				save_del.close();
			}
			break;

			case 3: char temp;
			cout << _("\n\nAll favorites are going to be deleted. Are you sure?(y|n) ");
			cin >> temp;
				

			if(temp=='y')
			{
				ofstream del_all;
				del_all.open(param.c_str());
				del_all.close();
			}
			break;
			case 4: cout << _("\n\nWhich favorite server do you want to start?\n\n");
			for (int i = 0; i<=j;i++)
			{
				cout << (i+1) << "\t" <<  input[OFF_N][i] << endl;
			}
				
			cout << " : ";
			cin >> choice;

			choice--;
				
			if(choice > j)
			{
				cout << _("\n\nThis entry does not exist!");
				break;
			}
			else
			{	
				cout << _("\n\nStarting favorite...");	
					
				cout << flush; //the message should come before the server starts ;)
		
				system_check(system(input[OFF_C][choice].c_str()));
			}
			break;
			case 5: cout << _("\n\nFrom which favorite server do you want to create a .desktop entry?\n\n");
			for (int i = 0; i<=j;i++)
			{
				cout << (i+1) << "\t" <<  input[OFF_N][i] << endl;
			}
				
			cout << " : ";
			cin >> choice;

			choice--;
				
			if(choice > j)
			{
				cout << _("\n\nThis entry does not exist!");
				break;
			}
			else
			{
				params = home_dir;	
				params += input[OFF_N][choice];
				params += ".desktop";
				ofstream desktop(params.c_str());
					
				desktop << "[Desktop Entry]\n";
				desktop << "Encoding=UTF-8\n";
				desktop << "Name=" << input[OFF_N][choice] << endl;
				desktop << "Comment=Executes " << input[choice][OFF_N] << " in a terminal.\n";
				desktop << "Exec=steamserverutil --exec-fav " << choice << endl;
				desktop << "Icon=/usr/share/pixmaps/steamserverutil/icon.png\n";
				desktop << "Terminal=true\n";
				desktop << "Type=Application\n";
				desktop << "Categories=Application;Network\n";
					
				desktop.close();
					
				cout << _("\n\nSucces!\nSaved to: \"")<< params << "\"\n";
			}
			break;
			case 6: exit=true;
			break;
			default: cout << _("\n\nTry again ;)\n");
		} 
		

		save.close();
	
	}
	
}


//to update the client
void update_client()
{
	cout << _("Updating Steam Linux Client\n\n");
	
	string _update;
	
	_update = workdir + "/" + "./steam -command update";
	
	system_check(system(_update.c_str()));

	cout << _("\n\nUpdated\n\n");
}

void delete_server()
{
	cout << _("\nWhich server do you wish to delete?\n");
	
	//if(!is_installed())
	//	return;
	

	is_installed();
	cout << i << _(".\tDelete the hole content\n");
	cout << ++i << _(".\tExit\n");
	cout << " : ";
	unsigned int choice;
	cin >> choice;
	
	cin.ignore();


	if(choice > i || choice == i)
		return;

	--i; //to "delete" exit command
	
	
	param = "rm -rf " + workdir;
	
	if(choice == i)
		installed = true;
	else
	{
		param += "/";
		server_path(server[choice]);
	}
	

	
	
	
	
	if(installed)
	{
		cout << _("\nAre you sure?(y|n) ");
		char sure;
		
		cin >> sure;
		
		if(sure == 'y')
		{
			cout << _("\n\nDeleting, this can take a few moments...");
			cout << flush;
			
			system_check(system(param.c_str()));
			
			if(choice == i)
			{
				param = "rm -rf " + home_dir + "/.steamserverutil";
				system_check(system(param.c_str()));
				return;
			}
			
			cout << _("\n\nRemoving server from file installed_servers...");
			cout << flush;
			
			
			//here we are using arrays because it is easier than std::string
			//and we are sure that it do not need hold more than 20 or 25
			char array[20];
			char array_2[25][20];
			
			
			switch(server[choice])
			{
				case 1:	strcpy(array, "css");
				break;
				case 2:	strcpy(array, "dods");
				break;
				case 3:	strcpy(array, "garrysmod");
				break;
				case 4:	strcpy(array, "hl2mp");
				break;
				case 5:	strcpy(array, "tf");
				break;
				case 6:	strcpy(array, "cstrike");
				break;
				case 7:	strcpy(array, "czero");
				break;
				case 8:	strcpy(array, "dmc");
				break;
				case 9:	strcpy(array, "dod");
				break;
				case 10:strcpy(array, "gearbox");
				break;
				case 11:strcpy(array, "ricochet");
				break;
				case 12:strcpy(array, "tfc");
				break;
				case 13:strcpy(array, "darkmessiah");
				break;
				case 14:strcpy(array, "chivalry");
				break;
				case 15:strcpy(array, "diprip");
				break;
				case 16:strcpy(array, "insurgency");
				break;
				case 17:strcpy(array, "synergy");
				break;
				case 18:strcpy(array, "zps");
				break;
				case 19:strcpy(array, "esmod");
				break;
				case 20:strcpy(array, "l4d");
				break;
			}
		
			ifstream read;
			
			//set workdir path
			param = workdir + "/installed_servers";
			
			read.open(param.c_str()); //to save the servers in memory
		
			
			int counter = 0;
			
			while(!read.eof()) //saves in memory
			{
				read.getline(array_2[counter], 20);
				counter ++;
			}
			
			read.close();
		
			ofstream out;
			out.open(param.c_str()); //writes complete new file
			
			for(int i = 0; i < counter; i++)
			{
				if(!strcmp(array_2[i], array)); //nothing to do
					
				else
					out << endl << array_2[i];
			}
			
			out.close();
			
			cout << _("\nSucces!\n\n");
		
		}
		
	}
	
	--i; //too remove last "server" which was the option to delete hole content
	
	
	
	
	
}



//about box
void about()
{
	cout << endl << endl << endl;
	
	cout << "steamserverutil " << _version << " steamserverutil.googlecode.com" << endl;
	cout << "Copyright (C) 2008, 2009  MJ, hanfman_cool@web.de\n";
	cout << _("steamserverutil comes with ABSOLUTELY NO WARRANTY\n\n");
			
	cout << _("For the GPL v2 license look \"http://www.gnu.org/licenses/gpl-2.0.html\"\n");
	cout << _("For the source code look \"http://code.google.com/p/steamserverutil/source/browse/\"\n"); 
}
